export class CreateDatabase {
  create_mysql(str: string): string {
    const sourse = str.split('\n')
    let temp = 'CREATE TABLE `{table}` ('
    const title: string[] = this.mysqlTitle(sourse[0])
    temp = temp.replace('{table}', title[0])
    temp = temp + '\n' + '	`id` INT(11) NOT NULL AUTO_INCREMENT,'
    temp = temp + '\n' + '{field}'
    temp = temp + "	`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,"
    temp = temp + '\n' + '	`updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,'
    temp = temp + '\n' + '	PRIMARY KEY (`id`) USING BTREE'
    temp = temp + '\n' + ')'
    temp = temp + '\n' + "COMMENT='" + title[1] + "'"
    temp = temp + '\n' + "COLLATE='utf8_general_ci'"
    temp = temp + '\n' + 'ENGINE=InnoDB'
    temp = temp + '\n' + 'ROW_FORMAT=DYNAMIC'
    temp = temp + '\n' + ';'
    temp = temp.replace('{field}', this.mysqlField(sourse))
    return temp
  }

  private mysqlTitle(str: string) {
    if (str.indexOf('--') > -1) {
      const temp = str.split(' ')
      return [temp[1], temp[2]]
    }
    return ['', '']
  }

  private mysqlField(str: string[]) {
    let temp = ''
    for (let i = 1; i < str.length; i++) {
      const t = str[i].split(' ')
      temp = temp + '	`' + t[0] + '`'
      switch (t[1]) {
        case 'var':
          temp = temp + ' VARCHAR(255)  NULL DEFAULT NULL' + " COMMENT '" + t[2] + "'"
          break
        case 'int':
          temp = temp + ' INT(11)  NULL DEFAULT 0' + " COMMENT '" + t[2] + "'"
          break
        case 'text':
          temp = temp + ' TEXT NULL DEFAULT NULL' + " COMMENT '" + t[2] + "'"
          break
        case 'time':
          temp = temp + ' TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP' + " COMMENT '" + t[2] + "'"
          break
        case 'date':
          temp = temp + ' DATE NULL DEFAULT NULL' + " COMMENT '" + t[2] + "'"
          break
        case 'datetime':
          temp = temp + ' DATETIME NULL DEFAULT NULL' + " COMMENT '" + t[2] + "'"
          break
        case 'float':
          temp = temp + ' FLOAT(10,2) NULL DEFAULT NULL' + " COMMENT '" + t[2] + "'"
          break
        case 'decimal':
          temp = temp + ' DECIMAL(10,2) NULL DEFAULT NULL' + " COMMENT '" + t[2] + "'"
          break
        default:
          temp = temp + ' VARCHAR(255) NULL DEFAULT NULL' + " COMMENT '" + t[2] + "'"
          break
      }
      temp = temp + ',' + '\n'
    }
    return temp
  }
}

/**
 * 
CREATE TABLE `admin` (
	`id` INT(10) NOT NULL AUTO_INCREMENT,

	`created_at` TIMESTAMP NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
	`updated_at` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
	PRIMARY KEY (`id`) USING BTREE,
)
COMMENT='后台用户表'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DYNAMIC
;
 * 
 * 
 */
